package com.gary.design.creatation.singleton;

public class Person {
    private String name;
    private String age;

    // 懒汉
    private volatile static Person instance;
    // 饿汉
//    private volatile static Person instance = new Person();

    //构造器私有，外部不能实例化
    private Person() {
        System.out.println("创建了Person");
    }


    //提供给外部的方法
    //1、public static synchronized Person getInstance() 锁太大
    //2、双重检查锁+内存可见性（设计模式）
    public static Person getInstance() {
        //如果没有再去创建
        if (instance == null) {
            synchronized (Person.class) {
                if(instance == null){
                    //多线程问题
                    instance = new Person();
                }
            }
        }
        return instance;
    }
}
